package com.huacai.bloglike.service.impl;

import java.util.List;
import com.huacai.common.utils.DateUtils;
import com.huacai.common.utils.uuid.IdUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huacai.bloglike.mapper.BloglikeMapper;
import com.huacai.bloglike.domain.Bloglike;
import com.huacai.bloglike.service.IBloglikeService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import static com.huacai.common.utils.SecurityUtils.getUserId;

/**
 * 博客点赞Service业务层处理
 *
 * @author lundao
 * @date 2025-09-12
 */
@Service
public  class BloglikeServiceImpl implements IBloglikeService
{
    @Autowired
    private BloglikeMapper bloglikeMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询博客点赞
     *
     * @param bloglikeId 博客点赞主键
     * @return 博客点赞
     */

    /**
     * 判断当前用户有没有对某个博客点过赞
     * @param blogId
     * @return
     */
    /**
     * 判断当前用户有没有对某个博客点过赞
     */
    @Override
    public int selectBlogLikeByUserIdAndBlogId(String blogId) {
        Integer userId = Math.toIntExact(getUserId());
        return bloglikeMapper.selectBlogLikeByUserIdAndBlogId(blogId, userId);
    }



    /**
     * 查询博客点赞列表
     *
     * @param bloglike 博客点赞
     * @return 博客点赞
     */
    @Override
    public List<Bloglike> selectBloglikeList(Bloglike bloglike)
    {
        return bloglikeMapper.selectBloglikeList(bloglike);
    }


    /**
     * 新增博客点赞
     *
     * @param bloglike 博客点赞
     * @return 结果
     */
    @Override
    public int insertBloglike(Bloglike bloglike)
    {
        bloglike.setCreateTime(DateUtils.getNowDate());
        bloglike.setBloglikeId(IdUtils.randomUUID());
        bloglike.setUserId(Math.toIntExact(getUserId()));
        return bloglikeMapper.insertBloglike(bloglike);
    }

    /**
     * 批量新增博客点赞
     *
     * @param bloglikes 博客点赞List
     * @return 结果
     */
    @Override
    public int batchInsertBloglike(List<Bloglike> bloglikes)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(bloglikes)) {
            try {
                for (int i = 0; i < bloglikes.size(); i++) {
                    int row = bloglikeMapper.insertBloglike(bloglikes.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == bloglikes.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改博客点赞
     *
     * @param bloglike 博客点赞
     * @return 结果
     */
    @Override
    public int updateBloglike(Bloglike bloglike)
    {
        return bloglikeMapper.updateBloglike(bloglike);
    }

    /**
     * 批量删除博客点赞
     *
     * @param bloglikeIds 需要删除的博客点赞主键
     * @return 结果
     */
    @Override
    public int deleteBloglikeByBloglikeIds(String[] bloglikeIds)
    {
        return bloglikeMapper.deleteBloglikeByBloglikeIds(bloglikeIds);
    }

    /**
     * 删除博客点赞信息
     *
     * @param bloglikeId 博客点赞主键
     * @return 结果
     */
    @Override
    public int deleteBloglikeByBloglikeId(String bloglikeId)
    {
        return bloglikeMapper.deleteBloglikeByBloglikeId(bloglikeId);
    }
    @Override
    public Bloglike selectBloglikeByBloglikeId(String bloglikeId){
        return bloglikeMapper.selectBloglikeByBloglikeId(bloglikeId);
    };
    /**
     * 根据userId和blogId删除博客点赞
     * @param blogId
     * @return
     */
    @Override
    public int deleteBlogLikeByBlogIdAndUserId(String blogId) {
        Integer userId = Math.toIntExact(getUserId());
        return bloglikeMapper.deleteBlogLikeByBlogIdAndUserId(userId, blogId);
    }
}
